package querylog.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import querylog.bean.LogBody;
import querylog.bean.LogHead;
import querylog.bean.PageBean;
import querylog.service.LogService;
import querylog.util.AppUtil;

/**
 * @typeName QueryLogServlet
 * @Description: 日志查询servlet，控制跳转
 * @author wangxy
 * @date 2017年11月30日下午2:44:04
 * @version v1.0
 */
public class QueryLogServlet extends BaseServlet {
	/**
	 * @Title showLogHeads
	 * @Description 展示日志概要信息
	 * @param @param request
	 * @param @param response
	 * @param @throws ServletException
	 * @param @throws IOException
	 */
	public void showLogHeads(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//接受请求参数
		String seqNo = request.getParameter("seqNo");				//流水号
		String tellerNo = request.getParameter("tellerNo");			//柜员号
		String indate = request.getParameter("inDate");				//做交易时间。入库时间，自然日
		String hostid = request.getParameter("RadioGroup1");		//交易的环境id
		String tranCode = request.getParameter("tranCode");			//接口缩写，如059322
		String condition = request.getParameter("condition");		//查询条件。只在翻页时得到值
		String currentPageFromPage = request.getParameter("currentPage");	//当前页码。只在翻页时得到值
		
		//分页初始化。首查时设为1.每页记录20条
		int currentPage = 1;
		int pageSize = 20;
		
		LogService ls = new LogService();

		//首查。首查：需要生成条件sql字符串。（翻页：页面会传pageBean中的值）
		if (currentPageFromPage==null || "".equals(currentPageFromPage)){
			condition = ls.generateConditionSql(seqNo, tellerNo, indate, hostid,tranCode);			
		}else {
			//翻页。翻页时：request.getParameter得到的都是页数是String格式。首查时：初始值1
			currentPage = Integer.parseInt(currentPageFromPage);
		}
		
		
		PageBean pb = ls.queryLogHeadByCondition(condition,currentPage,pageSize);
		//符合条件的loghead数量
		switch (pb.getLogHeads().size()) {
		//没有符合的日志头
		case 0:
			String errormsg = "查询无返回结果";			//错误信息
			request.getSession().setAttribute("ERROR", errormsg);
			request.getRequestDispatcher("/error.jsp").forward(request, response);
			break;
		//只有一个符合的日志头。直接给出全部日志信息。
		case 1:		
			LogHead loghead = pb.getLogHeads().get(0);
			loghead.setReqinfo(AppUtil.formatXML(loghead.getReqinfo()));
			loghead.setRespinfo(AppUtil.formatXML(loghead.getRespinfo()));
			String onlyOneSeqno =loghead.getSeqno();
			List<List<String>> logbodylist = ls.queryLogBody(onlyOneSeqno);
			//System.out.println(logbodylist);			
			request.getSession().setAttribute("LogHead", loghead);
			request.getSession().setAttribute("LogBodyList", logbodylist);
			request.getRequestDispatcher("/showLogAll.jsp").forward(request, response);
			break;
		
		//根据查询条件有多个符合条件的交易日志信息，跳转显示符合条件的交易日志信息列表
		default:
			request.getSession().setAttribute("PageLogHeads", pb);
			request.getRequestDispatcher("/showLogHeadList.jsp").forward(request, response);
			break;
		}
		
	}
	
	/**
	 * @Title showLogAll
	 * @Description 展示全部日志信息
	 * @param @param request
	 * @param @param response
	 * @param @throws ServletException
	 * @param @throws IOException
	 */
	public void showLogAll(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String seqNo = request.getParameter("seqNo");
		
		LogService ls = new LogService();
		List<List<String>> logbodylist = ls.queryLogBody(seqNo);
		LogHead loghead = ls.queryLogHead(seqNo);
		
		request.getSession().setAttribute("LogHead", loghead);
		request.getSession().setAttribute("LogBodyList", logbodylist);
		request.getRequestDispatcher("/showLogAll.jsp").forward(request, response);
		
		
	}

}
